package com.ccc.dingtalk.listener;

import com.ccc.dingtalk.service.dingtalk.TokenService;
import com.ccc.dingtalk.service.dingtalk.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 程序启动事件
 *
 * @author superC
 * @version 2.0.0
 * @date 2023/8/24
 */

@Component
public class AppStartEvent {
    protected static final Logger logger = LoggerFactory.getLogger(AppStartEvent.class);

    @Autowired
    private TokenService tokenService;

    @Autowired
    private UserService userService;

    /**
     * 初始化数据
     */
    @PostConstruct
    public void initData() {
        // 重新获取钉钉所有用户存储至DB
        this.resetDbUsers();
    }

    public void resetDbUsers() {
        try {
            userService.getAllUsersAndStoreDb(tokenService.getAccessToken());
        } catch (Exception e) {
            logger.error("【调用钉钉接口】-> 【捕获异常-获取钉钉用户】\r\n异常记录：", e);
        }
    }

}
